###R-Script: Never Let a Serious Crisis Go to Waste: The Introduction of Supplemental Carbon Taxes in Europe ###

##Required R-packages 
if (!require(QCA)) install.packages("QCA")
if (!require(plyr)) install.packages("plyr")
if (!require(foreign)) install.packages("foreign")
if (!require(haven)) install.packages("haven")
if (!require(readxl)) install.packages("readxl")
if (!require(xlsx)) install.packages("xlsx")
if (!require(tidyverse))install.packages("tidyverse")
if (!require(ggrepel))install.packages("ggrepel")
if (!require(ggthemes))install.packages("ggthemes")
if (!require(gridExtra))install.packages("gridExtra")
if (!require(ggExtra))install.packages("ggExtra")

library(QCA)
library(plyr)
library(foreign)
library(haven)
library(readxl)
library(xlsx)
library(ggplot2)
library(ggrepel)
library(ggthemes)
library(gridExtra)

## Set working directory and load dataset
setwd("...")
QCA_data<-read_excel("QCA_Dataset.xlsx")


## Calibration
QCA_data$Problem_1<-calibrate(QCA_data$Emission_targets, type = "fuzzy", method = "direct", thresholds = "e=0, c=5.0, i=10.0",logistic = F)
QCA_data$Problem_2<-calibrate(QCA_data$Fiscal_deficit, type = "fuzzy", method = "direct", thresholds = "e=0, c=-3.0, i=-9.94",logistic = F)
QCA_data$Politics <- calibrate(QCA_data$Green_government, type = "fuzzy", method = "direct", thresholds = "e=0, c=3.41, i=12.43", logistic = F)


## Prepare dataset for analysis
QCA_data_2<-data.frame(QCA_data$Country, QCA_data$Carbon_tax, QCA_data$Problem_1, QCA_data$Problem_2, QCA_data$Politics, QCA_data$Policy_window)
detach("package:dplyr")
QCA_final<- rename(QCA_data_2, c("QCA_data.Country" = "country", "QCA_data.Carbon_tax" = "carbontax", "QCA_data.Problem_1" ="problem1", "QCA_data.Problem_2" = "problem2", "QCA_data.Politics" = "politics", "QCA_data.Policy_window" = "window"))


## Analysis of necessary conditions
n1<-superSubset(QCA_final, outcome = "carbontax", conditions = "problem1, problem2, politics, window", incl.cut = 0.8)
n1


## Analysis of sufficient conditions
Thtable_QCA1 <- truthTable(QCA_final, "carbontax", conditions = "problem1, problem2, politics, window" ,incl.cut = 0.75, show.cases = TRUE, sort.by="incl")
Thtable_QCA1

  #Compelx solution
  res1<-minimize(Thtable_QCA1, details = TRUE, show.cases = TRUE)
  res1

  #Intermediate solution
  res2<-minimize(Thtable_QCA1, include ="?", show.cases=T, details=T, dir.exp = "1,1,1,1")
  res2


  #Most parsimouious solution
  res3<-minimize(Thtable_QCA1, include = "?", details = TRUE, show.cases = TRUE)
  res3
  
  
### Robustness-Checks ###

  ##Varying calibration problem stream climate +- 20%
  5*1.2
  10*1.2
  QCA_final$problem1_plus_20<-calibrate(QCA_data$Emission_targets, type = "fuzzy", method = "direct", thresholds = "e=0, c=6.0, i=12.0",logistic = F)
  5*0.8
  10*0.8
  QCA_final$problem1_minus_20<-calibrate(QCA_data$Emission_targets, type = "fuzzy", method = "direct", thresholds = "e=0, c=4.0, i=8.0",logistic = F)
  
  Thtable_QCA_R1 <- truthTable(QCA_final, "carbontax", conditions = "problem1_plus_20, problem2, politics, window" ,incl.cut = 0.75, show.cases = TRUE, sort.by="incl")
  res_R_1<-minimize(Thtable_QCA_R1, details = TRUE, show.cases = TRUE)
  res_R_1
  
  Thtable_QCA_R2 <- truthTable(QCA_final, "carbontax", conditions = "problem1_minus_20, problem2, politics, window" ,incl.cut = 0.75, show.cases = TRUE, sort.by="incl")
  res_R_2<-minimize(Thtable_QCA_R2, details = TRUE, show.cases = TRUE)
  res_R_2
  
  ##Varying calibration problem stream budget +- 20%
  -3*1.2
  -9.94*1.2
  QCA_final$problem2_plus_20<-calibrate(QCA_data$Fiscal_deficit, type = "fuzzy", method = "direct", thresholds = "e=0, c=-3.6, i=-11.9",logistic = F)
  -3*0.8
  -9.94*0.8
  QCA_final$problem2_minus_20<-calibrate(QCA_data$Fiscal_deficit, type = "fuzzy", method = "direct", thresholds = "e=0, c=-2.4, i=-8.0",logistic = F)
  
  Thtable_QCA_R3 <- truthTable(QCA_final, "carbontax", conditions = "problem1, problem2_plus_20, politics, window" ,incl.cut = 0.75, show.cases = TRUE, sort.by="incl")
  res_R_3<-minimize(Thtable_QCA_R3, details = TRUE, show.cases = TRUE)
  res_R_3
  
  Thtable_QCA_R4 <- truthTable(QCA_final, "carbontax", conditions = "problem1, problem2_minus_20, politics, window" ,incl.cut = 0.75, show.cases = TRUE, sort.by="incl")
  res_R_4<-minimize(Thtable_QCA_R4, details = TRUE, show.cases = TRUE)
  res_R_4
  
  ##Varying calibration political stream +- 20%
  3.41*1.2
  12.43*1.2
  QCA_final$politics_plus_20 <- calibrate(QCA_data$Green_government, type = "fuzzy", method = "direct", thresholds = "e=0, c=4.1, i=14.9", logistic = F)
  3.41*0.8
  12.43*0.8
  QCA_final$politics_minus_20 <- calibrate(QCA_data$Green_government, type = "fuzzy", method = "direct", thresholds = "e=0, c=2.7, i=9.9", logistic = F)
  
  Thtable_QCA_R5 <- truthTable(QCA_final, "carbontax", conditions = "problem1, problem2, politics_plus_20, window" ,incl.cut = 0.75, show.cases = TRUE, sort.by="incl")
  res_R_5<-minimize(Thtable_QCA_R5, details = TRUE, show.cases = TRUE)
  res_R_5
  
  Thtable_QCA_R6 <- truthTable(QCA_final, "carbontax", conditions = "problem1, problem2, politics_minus_20, window" ,incl.cut = 0.75, show.cases = TRUE, sort.by="incl")
  res_R_6<-minimize(Thtable_QCA_R6, details = TRUE, show.cases = TRUE)
  res_R_6